﻿var myv = new Vue({
    el: "#app",
    data: {
        Province: "",
        City: "",
        ShoppingCart: [],
        provinceList: [],
        cityList: []
    }
});


$(function () {
    //加载购物车
    (async function () {
        await setTimeout(() => {
            $.post("/GMShopMethod/ShoppingCart", {}, function (date) {
                myv.$data.ShoppingCart = date;
                if (date.State == 10) {
                    location = "/GMShop/LogIn";
                }
            })
        }, 10);
        await setTimeout(() => {
            CountFun();
        }, 500);
    })();

    //地址(市)下拉框
    $(".SelectChange").change(function () {
        $.post("/GMShopMethod/CityList", { city: myv.$data.Province }, function (data) {
            myv.$data.cityList = data;
            console.log(data);
            (async function () {
                await setTimeout(() => {
                    $("#City").children().eq(0).attr("selected", "selected");
                    myv.$data.City = $("#City").val();
                    console.log(myv.$data.City);
                }, 100);
            })();
        });
    });

    //地址(省)下拉框
    (function () {
        $.post("/GMShopMethod/ProvinceList", {}, function (data) {
            myv.$data.provinceList = data;
        });
    })();
    //清除购物车
    $(document).on("click", "#DeleteBut", function () {
        var shopIn = $(this).parent().parent().attr("id");
        $.post("/GMShopMethod/DelShopCar", { ShopId: shopIn }, function (data) {
            switch (data.State) {
                case 0:
                    alert(data.Massage);
                    break;
                case 1:
                    console.log("重定向")
                    location.reload();
                    break;
            }
        })
    });
    //提交按钮
    $("#submitBut").click(function () {

        var infoArr = new Array();
        infoArr[0] = $("#userName").val();
        infoArr[1] = $("#Phone").val();
        infoArr[2] = myv.$data.Province + myv.$data.City + $("#path").val();
        if (myv.$data.Province == "" || myv.$data.City == "" || $("#path").val() == "") return;
        for (var i = 0; i < infoArr.length; i++) {
            if (infoArr[i] == "") {
                return;
            }
        }

        $.post("/GMShopMethod/BuyGoods", { info: infoArr }, function (data) {
            alert(data.Massage);
        })
    });

    //更新计算
    function CountFun() {
        var shopArr = $(".ShopInfo");
        var money = 0;
        for (var i = 0; i < shopArr.length; i++) {
            money += parseInt($(".NewMoney").eq(i).text());
        }
        $("tfoot tr td span").eq(0).text(money);
        //节省
        var oldMoney = 0;
        for (var i = 0; i < shopArr.length; i++) {
            oldMoney += parseInt($(".OldMoney").eq(i).text());
        }
        $("tfoot tr td span").eq(1).text(oldMoney - money);
    }
})